let mongoose = require('mongoose');

let schema = new mongoose.Schema({
    count:{ //未领取次数
        type:Number,
        default:5
    },
    receiveCount:{  //已领取次数
        type:Number,
        default:0
    },
    lastRefreshTick:{ //最后刷新时间戳
        type:Number,
        default:0
    }
});

schema.methods={
    getMessage(){
        this._refresh();
        return {
            count:this.count,
            receiveCount:this.receiveCount,
            rewards:[{type:1,count:50}]
        };
    },
    //领取
    receive(options){
        if(this.receiveCount>=this.count){
            throw new Error('您今天的补给已经领取完了，请明天再来，谢谢！');
        }
        let rewards=[{type:1,count:50}];
        options.self.getGoods(rewards);
        this.receiveCount++;
        return rewards;
    },
    //刷新每日礼包
    _refresh(){
        let nowDate=new Date();
        let lastDate=new Date(this.lastRefreshTick);
        if(nowDate.toLocaleDateString()!==lastDate.toLocaleDateString()){
            this.count=5;
            this.receiveCount=0;
            this.lastRefreshTick=new Date().getTime();
        }
    }
};

module.exports = schema;